package com.zw.mizhe.api;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class ApiServiceManager {
    private static volatile ApiServiceManager instance = null;
    public ApiService apiService;
    public BaseDataService baseDataService;

    private ApiServiceManager() {
    }

    public static ApiServiceManager getInstance() {
        if (instance == null) {
            synchronized (ApiServiceManager.class) {
                if (instance == null) {
                    instance = new ApiServiceManager();
                }
            }
        }
        return instance;
    }

    public <T> void toSubscribe(Observable<T> o, Subscriber<T> s) {
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }

    public ApiService getApiService() {
        return apiService == null ? RetrofitManager.getInstance().getRetrofit()
                .create(ApiService.class) : apiService;
    }

    public BaseDataService getBaseDataService() {
        return baseDataService == null ? RetrofitManager.getInstance()
                .getRetrofit(UrlManager.RequestUrl.BASE_DATA_URL)
                .create(BaseDataService.class) : baseDataService;

    }
}